﻿using FireStarter.Base;
using FireStarterModeller.UI.Dialogs;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.Dialogs
{

    [TestFixture]
    public class TestOpenFileDialogExtendedTextBox
    {
        [TestFixtureSetUp]
        public void SetUpFixture()
        {
            GlobalUIRegistry.ControlFactory = new ControlFactoryWin();
            ClassDef.ClassDefs.Clear();
            ClassDef.ClassDefs.Add(new XmlClassDefsLoader(BOBroker.GetClassDefsXml(), new DtdLoader()).LoadClassDefs());
        }

        [SetUp]
        public void Setup()
        {
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [Test]
        public void Test_Construct()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------
            
            //---------------Execute Test ----------------------
            OpenFileDialogExtendedTextBox openFileExtendedDialog = new OpenFileDialogExtendedTextBox();
            //---------------Test Result -----------------------
            Assert.IsNotNull(openFileExtendedDialog);
            Assert.IsInstanceOf<OpenFileDialogExtendedTextBox>(openFileExtendedDialog);
        }

        [Test]
        public void Test_Button_WhenClicked_ShouldCauseShowDialogToExecute()
        {
            //---------------Set up test pack-------------------
            var openFileExtendedDialog = CreateOpenFileDialogExtendedTextBoxSpy();
            //---------------Assert Precondition----------------
            //openFileExtendedDialog.FileExtensions = "mdb";
            openFileExtendedDialog.FileExtensions.AddExtensionFilter("mdb", "Microsoft Access");
            openFileExtendedDialog.FileExtensions.AddExtensionFilter("accdb", "Microsoft Access 2007");
            openFileExtendedDialog.FileExtensions.AddExtensionFilter("mdf", "Microsoft SQL Server Express");
            //---------------Execute Test ----------------------
            openFileExtendedDialog.Button.PerformClick();
            //---------------Test Result -----------------------
            Assert.IsTrue(openFileExtendedDialog.ShowDialogWasCalled);
            Assert.IsTrue(openFileExtendedDialog.UpdateDialogResultWasCalled);
        }

        [Ignore("For visual testing only")] //TODO Andrew Russell 25 Oct 2010: Ignored Test - For visual testing only
        [RequiresSTA]
        [Test]
        public void Test_VisualTest()
        {
            //---------------Set up test pack-------------------
            var openFileExtendedDialog = CreateOpenFileDialogExtendedTextBox();
            //---------------Assert Precondition----------------
            //openFileExtendedDialog.FileExtensions = "mdb";
            openFileExtendedDialog.FileExtensions.AddExtensionFilter("mdb","Microsoft Access");
            openFileExtendedDialog.FileExtensions.AddExtensionFilter("accdb", "Microsoft Access 2007");
            openFileExtendedDialog.FileExtensions.AddExtensionFilter("mdf", "Microsoft SQL Server Express");
            //---------------Execute Test ----------------------
            openFileExtendedDialog.Button.PerformClick();
            //---------------Test Result -----------------------
            Assert.Fail("Test Not Yet Implemented");
        }

        protected virtual OpenFileDialogExtendedTextBox CreateOpenFileDialogExtendedTextBox()
        {
            return new OpenFileDialogExtendedTextBox();
        }

        protected virtual OpenFileDialogExtendedTextBoxSpy CreateOpenFileDialogExtendedTextBoxSpy()
        {
            return new OpenFileDialogExtendedTextBoxSpy();
        }
    }

    public class OpenFileDialogExtendedTextBoxSpy : OpenFileDialogExtendedTextBox
    {

        public bool ShowDialogWasCalled { get; set; }
        public bool UpdateDialogResultWasCalled { get; set; }

        protected override DialogResult ShowFileDialog()
        {
            this.ShowDialogWasCalled = true;
            return new DialogResult();
        }

        protected override void UpdateDialogResult(DialogResult result)
        {
            this.UpdateDialogResultWasCalled = true;
        }
    }

}
